package pl.veneo.wigprofit.ctrl {
	import pl.veneo.wigprofit.ctrl.events.ChartResizeEvent;
	import pl.veneo.wigprofit.ctrl.events.ModelUpdateEvent;
	import pl.veneo.wigprofit.model.ChartDataModel;
	import pl.veneo.wigprofit.view.ChartView;

	import flash.events.EventDispatcher;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartControler extends EventDispatcher {
		
		private var _model : ChartDataModel;
		private var _view : ChartView;
		

		public function ChartControler(model : ChartDataModel, view : ChartView) {
			
			super();
			_view = view;
			_view.addEventListener(ChartResizeEvent.ON_CHART_RESIZE, onForceChartUpdate);
			_model = model;
			_model.addEventListener(ModelUpdateEvent.ON_DATA_UPDATED, onModelUpdated);
			_model.updateData();
		}

		private function onForceChartUpdate(event : ChartResizeEvent) : void {
			
			if(!event.force)
			_view.updateChartForPeriod(_model.getDataForPeriod(event.start, event.end));
			else
			_view.buildChartForPeriod(_model.getDataForPeriod(event.start, event.end));
			
		}

		public function onModelUpdated(event : ModelUpdateEvent) : void {
			
			_model.removeEventListener(ModelUpdateEvent.ON_DATA_UPDATED, onModelUpdated);
			//var start: Date = new Date(2011,2,0);
			_view.buildChartForPeriod(_model.getDataForPeriod(event.start, event.end));
			
		}
	}
}
